package com.ruoyi.pg.answerSheet.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pg.answerSheet.vo.PgAnswerVo;
import com.ruoyi.pg.answerSheet.service.IPgAnswerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 答卷详情Controller
 *
 * @author Ming
 * @date 2025-08-22
 */
@Api(tags = "答卷详情Controller")
@RestController
@RequestMapping("/answerSheet/answer")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class PgAnswerController extends BaseController {

    private final IPgAnswerService pgAnswerService;

    @ApiOperation("查询答卷详情列表")
    @GetMapping("/list")
    public TableDataInfo<PgAnswerVo> list(PgAnswerVo entity) {
        return pgAnswerService.queryList(entity);
    }

    @ApiOperation("查询答卷详情所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(PgAnswerVo entity) {
        return AjaxResult.success("查询成功", pgAnswerService.queryAll(entity));
    }

    @ApiOperation("导出答卷详情列表")
    @Log(title = "答卷详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PgAnswerVo entity) {
        List<PgAnswerVo> list = pgAnswerService.queryAll(entity);
        ExcelUtil<PgAnswerVo> util = new ExcelUtil<>(PgAnswerVo.class);
        util.exportExcel(response, list, "答卷详情数据");
    }

    @ApiOperation("获取答卷详情详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", pgAnswerService.queryById(id));
    }

    @ApiOperation("新增答卷详情")
    @Log(title = "答卷详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PgAnswerVo entity) {
        return toAjax(pgAnswerService.addOrEdit(entity));
    }

    @ApiOperation("新增答卷详情")
    @Log(title = "答卷详情", businessType = BusinessType.INSERT)
    @PostMapping("/saveBatch")
    public AjaxResult saveBatch(@RequestBody PgAnswerVo entity) {
        pgAnswerService.saveBatch(entity);
        return AjaxResult.success("新增成功");
    }

    @ApiOperation("修改答卷详情")
    @Log(title = "答卷详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PgAnswerVo entity) {
        return toAjax(pgAnswerService.addOrEdit(entity));
    }

    @ApiOperation("删除答卷详情")
    @Log(title = "答卷详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pgAnswerService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
